import request from '../../../api/request'
import { setItem } from '../../../utils/storage'
import { Dispatch } from 'redux'
import { CODE_SEND, USER_LOGIN, LOGIN_OUT } from './actionType'
const getCode = (data: {}) => ({ type: CODE_SEND, data })
export const codeSend =
  (mobile: string) => async (dispatch: (getCode: any) => void) => {
    const { data } = await request({
      url: '/v1_0/sms/codes/' + mobile,
    })
    dispatch(getCode(data))
  }

export const LoginUser =
  (data: { mobile: string; code: string }) => async (dispatch: Dispatch) => {
    const res = await request({
      url: '/v1_0/authorizations',
      method: 'post',
      data,
    })
    setItem(res.data.data)
    return dispatch({ type: USER_LOGIN, data: res.data })
  }

export const logout = () => ({ type: LOGIN_OUT })
